package piwan.ai;

import piwan.*;

public class PlayerInfo {

	private double tab[][][][] = new double[2][2][5][6]; // attack/defence, 1 or
	// more defSoldiers,
	// number of
	// attackingSoldiers,
	// probabilities
	private int from = 1;
	private Player p;
	private int freq[][][] = new int[2][2][5]; // attack/defence, 1 or more
	private int numRecorded;

	// defSoldiers, frequency of
	// games played with a certain
	// number
	public PlayerInfo(Player p) {
		this.p = p;
	}

	public int combatPlay( boolean playerIsAtt, int nDefTroops, int nAttTroops) {

		int isAtt = (playerIsAtt)? 0 : 1;
		int nDef = (nDefTroops ==1)? 0: 1;
		int nAtt = (nAttTroops > 6) ? 6 - 2 : nAttTroops - 2;
		
		double useWhat[] = new double[5];
		double t[] = new double [5];

		/*
		int num[][] = new int [2][2]; // number of recorded attacks against one ..

		for (int i = 0; i < freq.length; i++) {
			for (int j = 0; j < freq[0].length; j++) {
				for (int k = 0; k < freq[0][0].length; k++) {
					numRecorded += freq[i][j][k];
					num[i][j]+= freq[i][j][k];
				}
			}
		}
		*/
		//System.out.println(numRecorded);

		
		useWhat[0] = 1 - numRecorded / (double) (2 * (numRecorded + 10));
		for (int i = 1; i<5; i++)
			useWhat[i] = (1-useWhat[0])/6.0;
		useWhat[2* isAtt + nDef +1] = (1-useWhat[0])/2.0;  //A1, A2, D1, D2
		
		double sum = 0;
		double a = Math.random();

		//System.out.println("using "+a);
		
		for (int i = 0; i < useWhat.length; i++) {
			sum += useWhat[i];
			if (a < sum) {
				switch(i){
				case 0: t = createBestProbabilities(from,nAtt+2); break;
				case 1: t = tab[0][0][nAtt];break;
				case 2: t = tab[0][1][nAtt];break;
				case 3: t = tab[1][0][nAtt];break;
				case 4: t = tab[1][1][nAtt];break;
				}
				break;
				
			}
			
		}
		
		if(playerIsAtt){
			t = createBestProbabilities(from,nAtt+2);	//because attacker does not want to guess
		}
		
		double b = Math.random();
		sum = 0;
		//System.out.print("guessing"+b);
		for (int i = 0; i < nAtt+2; i++) {
			sum += t[i];
			if (b < sum) {
				//System.out.println("   "+(from+i));
				return from + i;
			}
		}
		return nAtt+2;
	}
	static private double[] createBestProbabilities(int from, int to) {
		double[] tab = new double[to + 1 - from];

		double ptotal = 0;
		for (int i = from; i <= to; i++) {
			ptotal += 2 / (double) (i + 1);
		}

		for (int i = from; i <= to; i++) {
			tab[i - from] = 2 / (double) (i + 1) / ptotal;
		}
		
		//System.out.println("best" +tab[0]);
		return (tab);
	}
	public void updateTab(int defSoldiers, int attackSoldiers, int number,
			boolean isAttacking) {

		int att = (isAttacking) ? 0 : 1;
		int numD = (defSoldiers == 1) ? 0 : 1;
		int numA = (attackSoldiers > 6) ? 6 - 2 : attackSoldiers - 2;

		for (int i = 0; i < numA + 2; i++) {
			tab[att][numD][numA][i] *= freq[att][numD][numA]							//errror outOfBound
					/ (double) (freq[att][numD][numA] + 1);
			tab[att][numD][numA][i] = (i + from == number) ? tab[att][numD][numA][i]
					+ 1 / (double) (freq[att][numD][numA] + 1)
					: tab[att][numD][numA][i];
		}
		freq[att][numD][numA]++;
		numRecorded++;
	}

	public void outprintTab() {

		for (int i = 0; i < 6; i++) {
			System.out.print((from + i) + ": " + tab[0][0][4][i] + ", ");
		}
	}


}
